<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文章列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../css/oksub.css">
    <script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
    <!--模糊搜索区域-->
    <div class="layui-row">
        <form class="layui-form layui-col-md12 ok-search">
            <input class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime" name="startTime">
            <input class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime" name="endTime">
            <button class="layui-btn" lay-submit="" lay-filter="search">
                <i class="layui-icon layui-icon-search"></i>
            </button>
        </form>
    </div>
    <!--数据表格-->
    <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script>
    layui.use(["element", "table", "form", "jquery", "laydate", "layer", "okLayer", "okUtils", "okMock"], function () {
        let table = layui.table;
        let form = layui.form;
        let util = layui.util;
        let layer = layui.layer;
        let $ = layui.jquery;
        let laydate = layui.laydate;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;
        let okMock = layui.okMock;
        okLoading.close();
        //提交
        lineTools(table,layer);
        //行工具
        formSubimt(form,$,layer);
        //数据表单
        dataForm(table);



        function formSubimt(form,$,layer){
            form.on('submit(formDemo)', function(data){
                const fromdata=data.field //当前容器的全部表单字段，名值对形式：{name: value}
                var parms = JSON.stringify(fromdata);
                $.ajax({
                    url:"/depts/update",
                    type: "post",
                    //     contentType: "application/json;charset=utf-8",
                    data: {"params":parms},
                    dataType: "json",
                    success: function (d) {
                        if (d.code==0) {
                            layer.alert("操作成功");
                            window.location.reload();
                        } else {
                            layer.alert("操作失败");
                        }
                    }
                });
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });
        }


        function lineTools(table,layer) {
            table.on('tool(tableFilter)', function (obj) {
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'edit') { //查看
                    $('input[name=deptid]').val(data.deptid);
                    $('input[name=name]').val(data.name);
                    $('input[name=loader]').val(data.loader);
                    $('input[name=email]').val(data.email);
                    $('input[name=createby]').val(data.createby);
                    $('input[name=createdate]').val(data.createdate);
                    layer.open({
                        type: 1,
                        content: $('#bianji') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                }else if(layEvent === 'del'){ //删除
                    layer.confirm('真的删除行么', function(index){
                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        layer.close(index);
                        //向服务端发送删除指令
                        $.post('/depts/delete',{'id':data.deptid},function (d) {
                            console.log(d)
                        },'json')
                    });
                }

            })
        }


        function dataForm(table) {
             table.render({
                elem: "#tableId",
                url: '/depts/list',
                limit: 3,
                page: true,
                even: true,
                toolbar: "#toolbarTpl",
                size: "sm",
                cols: [[
                    {type: "checkbox", fixed: "left"},
                    {field: "deptid", title: "ID", sort: true},
                    {field: "name", title: "部门名称"},
                    {field: "sort", title: "排序"},
                    {field: "loader", title: "领导"},
                    {field: "phone", title: "电话"},
                    {field: "email", title: "邮箱"},
                    {field: "createby", title: "创建者"},
                    {field: "createdate", title: "创建时间"},
                    {field: "lastmodify", title: "上次修改时间"},
                    {field: "parentid", title: "所属父级"},
                    {title: "操作", width: 100, align: "center", fixed: "right", templet: "#operationTpl"}
                ]]

            });
        }


        laydate.render({
            elem: '#CreationTime' //指定元素
            ,format: 'yyyy/MM/dd' //可任意组合
        });

        form.on("submit(search)", function (data) {
            articleTable.reload({
                where: data.field,
                page: {curr: 1}
            });
            return false;
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "batchEnabled":
                    batchEnabled();
                    break;
                case "batchDisabled":
                    batchDisabled();
                    break;
                case "batchDel":
                    batchDel();
                    break;
                case "add":
                    add();
                    break;
            }
        });



        laydate.render({elem: "#startTime", type: "datetime"});
        laydate.render({elem: "#endTime", type: "datetime"});
    })
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">批量删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加部门</button>
    </div>
</script>

<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a href="javascript:" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
    <a href="javascript:" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>

<div id="bianji">
    <form class="layui-form" action="">
        <input type="text" name="deptid" style="display: none">
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称:</label>
            <div class="layui-input-block">
                <input type="text" name="name"  placeholder="请输入部门名称"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">领导名称:</label>
            <div class="layui-input-block">
                <input type="text" name="loader" placeholder="请输入领导名称"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮&nbsp;箱:</label>
            <div class="layui-input-block">
                <input type="text" name="email"  placeholder="请输入邮箱" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建者:</label>
            <div class="layui-input-block">
                <input type="text" name="createby"  placeholder="请输入创建者" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建时间:</label>
            <div class="layui-input-block">
                <input type="text" id="CreationTime" name="createdate"  placeholder="请输入创建时间" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo" >立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>
<link rel="stylesheet" href="../../css/okfrom.css">
